#!/bin/bash

# shellcheck disable=SC2120

# Provide source directive to shellcheck.
# shellcheck source=meta-facebook/meta-yosemite4/recipes-phosphor/state/phosphor-state-manager/power-cmd
source /usr/libexec/phosphor-state-manager/power-cmd
# shellcheck source=meta-facebook/meta-yosemite4/recipes-yosemite4/plat-tool/files/yosemite4-common-functions
source /usr/libexec/yosemite4-common-functions

CHASSIS_ID=$1
IO_EXP_SLOT_PWR_STATUS=$((CHASSIS_ID - 1))
GPIOCHIP_IO_EXP_SLOT_PWR_CTRL=$(basename "/sys/bus/i2c/devices/$SPIDER_BOARD_IO_EXP_BUS_NUM-00$IO_EXP_SLOT_PWR_CTRL_ADDR/"*gpiochip*)

check_i3c_hub_status()
{
    local target_slot="$1"
    local option="$2"
    local hub_path
    local offset_file
    local access_file
    local current_port
    local mask=1
    local i3c_hub_lock

    if [ "$target_slot" -lt 5 ]; then
        hub_path="/sys/kernel/debug/i3c-hub-0-*/reg"
        i3c_hub_lock="/tmp/i3c-hub0.lock"
        echo "Slot$target_slot on i3c hub 0."
    else
        hub_path="/sys/kernel/debug/i3c-hub-1-*/reg"
        i3c_hub_lock="/tmp/i3c-hub1.lock"
        echo "Slot$target_slot on i3c hub 1."
    fi

    for file in $hub_path/{offset,access}; do
        if [[ $file == *"/offset" ]]; then
            offset_file="$file"
        elif [[ $file == *"/access" ]]; then
            access_file="$file"
        fi
    done

    {
        flock -x 100
        echo "Unlock i3c hub register."
        echo 16 > "$offset_file"
        echo 105 > "$access_file"

        echo "Check slot${target_slot} i3c port."
        echo 18 > "$offset_file"
        current_port=$(cat "$access_file")
        if [ "$target_slot" -gt 4 ]; then
            ((slot=target_slot-4))
        fi

        check_bit=$(( (current_port >> (--slot)) & 1 ))
        if [ $check_bit -eq 0 ]; then
            if [ "$option" = "on" ]; then
                echo "Enable slot${target_slot} i3c port."
                mask=$((mask << (--slot)))
                current_port=$((current_port | mask))
                echo $current_port > "$access_file"
                cat "$access_file"
            fi
        else
            if [ "$option" = "off" ]; then
                echo "Disable slot${target_slot} i3c port."
                mask=$((mask << (--slot)))
                mask=$((~mask))
                current_port=$((current_port & mask))
                echo $current_port > "$access_file"
                cat "$access_file"
            fi
        fi
        echo "Lock i3c hub register."
        echo 16 > "$offset_file"
        echo 0 > "$access_file"
    } 100>$i3c_hub_lock
}

if [ "$CHASSIS_ID" -ge 1 ] && [ "$CHASSIS_ID" -le 8 ]
then
    chassis_status=$(gpio_get "$GPIOCHIP_IO_EXP_SLOT_PWR_CTRL" "$IO_EXP_SLOT_PWR_STATUS")
    if [ "$chassis_status" == "$STATE_ON" ]
    then
        echo "Chassis$CHASSIS_ID is on, check i3c hub is enabled"
        check_i3c_hub_status "$CHASSIS_ID" "on"
    elif [ "$chassis_status" == "$STATE_OFF" ]
    then
        echo "Chassis$CHASSIS_ID is off, check i3c hub is disabled"
        check_i3c_hub_status "$CHASSIS_ID" "off"
    else
        echo "Chassis$CHASSIS_ID status is unknown, skipped"
    fi
fi
